package com.scvtc.controller.system;

import com.scvtc.domain.ResResult;
import com.scvtc.domain.dto.system.menu.SystemMenuDto;
import com.scvtc.domain.dto.system.menu.SystemMenuUpdateDto;
import com.scvtc.domain.vo.PageVo;
import com.scvtc.service.system.SystemMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Digits;
import java.util.List;

import static com.scvtc.constants.SystemCheckedConstants.PAGE_CURRENT_ERROR;
import static com.scvtc.constants.SystemCheckedConstants.PAGE_SIZE_ERROR;
import static com.scvtc.constants.SystemConstants.PAGE_CURRENT;
import static com.scvtc.constants.SystemConstants.PAGE_SIZE;

/**
 * 菜单管理(SystemMenu)表控制层
 *
 * @author yiFei
 * @since 2022-04-13 17:38:27
 */
@Api(tags = "菜单管理(SystemMenu)")
@RestController
@Validated
@RequestMapping("systemMenu")
public class SystemMenuController {
    /**
     * 菜单管理(SystemMenu)对象
     */
    @Autowired
    private SystemMenuService systemMenuService;

    @GetMapping("/list")
    @ApiOperation("显示菜单")
    @PreAuthorize("hasAuthority('system:menu:list')")
    public ResResult<PageVo<List<SystemMenuUpdateDto>>> getAllMenus(
            @Digits(integer = 5, fraction = 1, message = PAGE_CURRENT_ERROR) @RequestParam(defaultValue = PAGE_CURRENT) Long current,
            @Digits(integer = 3, fraction = 0, message = PAGE_SIZE_ERROR) @RequestParam(defaultValue = PAGE_SIZE) Long size,
            @RequestParam(required = false) String name) {
        return systemMenuService.getAllMenus(current, size, name);
    }

    @GetMapping("/children/list")
    @ApiOperation("查询菜单（拥有子权限）")
    @PreAuthorize("hasAnyAuthority('system:menu:list','system:role:update')")
    public ResResult<List<SystemMenuDto>> getChildrenList() {
        return systemMenuService.getChildrenList();
    }

    @PutMapping("/")
    @ApiOperation("修改菜单信息")
    @PreAuthorize("hasAuthority('system:menu:update')")
    public ResResult<?> updateMenus(@RequestBody SystemMenuUpdateDto systemMenu) {
        return systemMenuService.updateMenus(systemMenu);
    }
}

